﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace IceMan.Domain
{
    public interface IStateStore
    {
        MainState Load();

        void Save(in MainState state);
    }

    public class JsonFileStore : IStateStore
    {
        private string dbFileName = "state.json";

        public MainState Load()
        {
            try
            {
                if (File.Exists(dbFileName))
                {
                    return JsonSerializer.Deserialize<MainState>(File.ReadAllText(dbFileName));
                }
                else
                {
                    return new MainState();
                }
            }
            catch (Exception ex)
            {

                return new MainState();
            }

        }

        public void Save(in MainState state)
        {
            lock (this)
            {
                File.WriteAllText(dbFileName, JsonSerializer.Serialize(state));
            }

        }
    }
}
